<?php
    /**
     * CookAdModel
     * VDuanziModel数据模型
     */
	class VduanziModel extends Model{
		protected $tableName = 'vduanzi';
		protected $error = '';
		protected $fields = array (
				0 => 'id',
				1 => 'title',
				2 => 'content',
				3 => 'image',
				4 => 'iamgeWidth',
				5 => 'iamgeHeight',
				6 => 'digg_count',
				7 => 'cTime',
				8 => 'isImage',
				9 => 'status',
				10 => 'other_app',
				11 => 'other_id',
				'_autoinc' => true,
				'_pk' => 'id'
		);
		/**
		 * _initialize
		 * 初始化函数
		 * @return void
		 */
		public function _initialize(){
            parent::_initialize();
		}
		
		public function getDunaziList(){
			$adList = $this->findAll();
			return $adList;
		}
		
	   /**
		 +----------------------------------------------------------
		 * 根据条件禁用表数据
		 * 
		 +----------------------------------------------------------
		 * @access public 
		 +----------------------------------------------------------
		 * @param mixed $condition 删除条件
		 +----------------------------------------------------------
		 * @return boolen
		 +----------------------------------------------------------
		 * @throws FcsException
		 +----------------------------------------------------------
		 */
		function forbid($condition,$table='')
		{
			$save['status']=0;
			if($this->where("id IN ({$condition}) AND status=1")->save($save)){
				return true;
			}else{
				return false;	
			}
		}

		/**
		 +----------------------------------------------------------
		 * 根据条件启用表数据
		 * 
		 +----------------------------------------------------------
		 * @access public 
		 +----------------------------------------------------------
		 * @param mixed $condition 删除条件
		 * @param string $table  数据表名
		 +----------------------------------------------------------
		 * @return boolen
		 +----------------------------------------------------------
		 * @throws FcsException
		 +----------------------------------------------------------
		 */
		function resume($condition,$table='')
		{
			$save['status']=1;
			if($this->where("id IN ({$condition}) AND status=0")->save($save)){
				return true;
			}else{
				return false;	
			}
		}
                
                function addAd()
		{
			$save['status']=1;
			if($this->where("id IN ({$condition}) AND status=0")->save($save)){
				return true;
			}else{
				return false;	
			}
		}
		
		public function saveCaptureData($data)
		{
			$map['other_id'] = $data['other_id'];
			$map['other_id'] = $data['other_app'];
			$other = $this->where($map)->select();
			if($other){
				return FALSE;
			}else{
				$res = $this->add($data);
				return $res;	
			}
		}
		
		public function isHaved($other_id, $other_app='duanzibao')
		{
			$map['other_id'] = $other_id;
			$map['other_app'] = $other_app;
			$other = $this->where($map)->select();
			if(!$other){
				return FALSE;
			}else{
				return true;
			}
		}
		
		public function isTheSameImage($imageUrl, $other_app='duanzibao')
		{
			$map['image'] = $imageUrl;
			$map['other_app'] = $other_app;
			$other = $this->where($map)->select();
			if(!$other){
				return FALSE;
			}else{
				return true;
			}
		}
		
		public function maxOther_id($other_app='duanzibao')
		{
			$map['other_app'] = $other_app;
			$other = $this->where($map)->order('other_id DESC')->select();
			$maxList = $other['0'];
			$maxid = $maxList['other_id'];
			if(!$maxid){
				return '0';
			}else{
				return $maxid;
			}
		}
		
		public function appendMaxOhter_id($numb, $other_app='duanzibao')
		{
			$map['other_app'] = $other_app;
			$other = $this->where($map)->order('other_id DESC')->select();
			$maxList = $other['0'];
			$maxid = $maxList['other_id'];
			$saveMap['id'] = $maxList['id'];
			$maxid_append = $maxid + $numb;
			$save['other_id'] = $maxid_append;
			$this->where($saveMap)->save($save);
		}
		
		public function maxTime($type)
		{
			$other = $this->where($type)->order('cTime ASC')->select();
			$maxList = $other['10'];
			$maxTime = $maxList['cTime'];
			if(!$maxTime){
				return '0';
			}else{
				return $maxTime;
			}
		}
		
		public function getVdById($id){
			$where = "id = $id";
			$data = $this->where($where)->select();
			return $data;
		}
        
        public function addDiggById($id){
            $map['id'] = $id;
			$digg_count = $this->where($map)->getField('digg_count');
			$save['digg_count'] = $digg_count+1;
			if($this->where($map)->save($save)){
				return true;
			}else{
				return false;
			}
		}
		
	    public function deleteDiggById($id){
            $map['id'] = $id;
			$digg_count = $this->where($map)->getField('digg_count');
			$save['digg_count'] = $digg_count-1;
			if($this->where($map)->save($save)){
				return true;
			}else{
				return false;
			}
		}
		
		// 根据id数组获取列表
		function getVdListByArr($arr) {
			for ($i=0; $i<count($arr); $i++) {
				$data = $this->getVdById($arr[$i]);
				$row = $data['0'];
				if ($data && $row)
					$list[] = $row;
			}
			return $list;
		}
	}
?>